package com.imooc.miaosha.service;

import com.imooc.miaosha.dao.GoodsDao;
import com.imooc.miaosha.domian.MiaoShaGoods;
import com.imooc.miaosha.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模块描述: <br>
 * (商品服务类)
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/18 23:35
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Service
public class GoodsService {

    public List<GoodsVO> listGoodsVo() {
        List<GoodsVO> goodsVOS = goodsDao.listGoodsVO();
        return goodsVOS;
    }

    public GoodsVO getGoodsVoByGoodsId(Long goodsId) {
        GoodsVO goodsVO = goodsDao.getGoodsVoByGoodsId(goodsId);
        return goodsVO;
    }

    public boolean reduceStock(GoodsVO goodsVo) {
        MiaoShaGoods miaoShaGoods = new MiaoShaGoods();
        miaoShaGoods.setGoodsId(goodsVo.getId());
        int ret = goodsDao.reduceStock(miaoShaGoods);
        return ret > 0;
    }


    public void resetStock(List<GoodsVO> goodsList) {
        for (GoodsVO goods : goodsList) {
            MiaoShaGoods g = new MiaoShaGoods();
            g.setGoodsId(goods.getId());
            g.setStockCount(goods.getStockCount());
            goodsDao.resetStock(g);
        }
    }

    @Autowired
    private GoodsDao goodsDao;

}
